package com.qk.mangement.controller;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * Date:2025/10/10
 * Author:zxc
 * Description
 */
@Controller
public class CookieAndSessionController {
    @GetMapping("/session")
    public void session(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.setAttribute("user", "zhangsan");
    }

    @GetMapping("/cookie")
    public void cookie(HttpServletResponse response){
        // Cookie  由服务器创建写回浏览器
        Cookie cookie = new Cookie("itheima", "111111111");
        // 设置cookie存储时间
        //  cookie.setMaxAge(60 * 60 * 24 * 7);
        // 设置cookie作用域
        cookie.setPath("/");
        response.addCookie( cookie);
    }


    @GetMapping("/cookie1")
    public void cookie1(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            String name = cookie.getName();
            String value = cookie.getValue();
            System.out.println(name + ":" + value);
        }
    }





}
